package com.stu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.stu.common.R;
import com.stu.mapper.*;
import com.stu.pojo.*;
import com.stu.service.IAccountService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;
import java.util.Objects;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author ${author}
 * @since 2023-12-20
 */
@Service
public class AccountServiceImpl extends ServiceImpl<AccountMapper, Account> implements IAccountService {
    @Autowired
    private StudentMapper studentMapper;
    @Autowired
    private StuAMapper stuAMapper;
    @Autowired
    private StudentDescMapper studentDescMapper;
    @Autowired
    private AccountMapper accountMapper;
    @Autowired
    private AccountFlowMapper accountFlowMapper;

    @Override
    @Transactional
    public boolean pay(AccountFlow accountFlow) {
        //获取account的id
        LambdaQueryWrapper<Account> qw = new LambdaQueryWrapper<>();
        qw.eq(Account::getName,accountFlow.getAccountName());
        Account account = accountMapper.selectOne(qw);
        Long accountId = account.getId();
        accountFlow.setAccountId(accountId);
        accountFlow.setId(getStuid());
        accountFlow.setIsDeleted(0);
        accountFlow.setDesctext("会员缴费");

        //生成一条flow记录
        int i = accountFlowMapper.insert(accountFlow);
        if (i > 0){
            //相应账户中的数量增加
            BigDecimal count = account.getCount();
            BigDecimal count1 = accountFlow.getCount();
            BigDecimal countR = count.add(count1);
            account.setCount(countR);
            accountMapper.updateById(account);
            //增加学生的缴费金额
            StudentDesc studentDesc = studentDescMapper.selectById(accountFlow.getStudentId());
            Double payCount = studentDesc.getPayCount();
            BigDecimal stuCount = new BigDecimal(Double.toString(payCount));
            BigDecimal countR2 = stuCount.add(count1);
            studentDesc.setPayCount(Double.parseDouble(countR2.toString()));
            studentDesc.setPayStatus("未完成");
            studentDescMapper.updateById(studentDesc);
        }
        // 修改记录中余额显示余额
        accountFlow.setYve(account.getCount());
        accountFlowMapper.updateById(accountFlow);
        return true;
    }

    /**
     * 退款业务
     * @param map
     * @return
     */
    @Override
    public R returnMoney(Map<String, String> map) {
        String stuid = map.get("id");
        String name = map.get("name");
        String accountName = map.get("account");
        String count = map.get("count");
        String desctext = map.get("desctext");
        String payment = map.get("payment");

        // 查询学生信息，确认学生信息是否存在
        Student student = studentMapper.selectById(stuid);
        if(Objects.isNull(student)){
            return R.error("学生不存在");
        }
        if(Objects.isNull(name)){
            return R.error("学生不存在");
        }
        // 查询账户信息
        LambdaQueryWrapper<Account> qw = new LambdaQueryWrapper<>();
        qw.eq(Account::getName,accountName);
        Account account = accountMapper.selectOne(qw);

        if(Objects.isNull(account)){
            return R.error("扣款账户不存在");
        }
        BigDecimal jinecount;
        BigDecimal zhuanghucount = account.getCount();
        if(StringUtils.isEmpty(count)){
            return R.error("退款金额不可为空");
        }
        jinecount = new BigDecimal(count);
        BigDecimal zuizhong = zhuanghucount.subtract(jinecount);
        if(zuizhong.compareTo(BigDecimal.ZERO) <= 0){
            return R.error("退款账户余额不足");
        }
        // 退款业务
        account.setCount(zuizhong);
        accountMapper.updateById(account);

        // 增加退款记录
        AccountFlow accountFlow = new AccountFlow();
        accountFlow.setAccountId(account.getId());
        accountFlow.setAccountName(accountName);
        accountFlow.setStudentId(Long.parseLong(stuid));
        accountFlow.setStudentName(name);
        accountFlow.setPayment(payment);
        accountFlow.setDesctext(desctext);
        accountFlow.setYve(account.getCount());
        accountFlow.setCount(jinecount.negate());
        accountFlowMapper.insert(accountFlow);

        return R.success("退款成功！");
    }

    /**
     * 支出金额
     * @param map
     * @return
     */
    @Override
    public R zhichuMoney(Map<String, String> map) {
        String accountId = map.get("id");
        String accountName = map.get("name");
        String count = map.get("count");
        String desctext = map.get("desctext");
        String payment = map.get("payment");

        // 查询账户信息
        LambdaQueryWrapper<Account> qw = new LambdaQueryWrapper<>();
        qw.eq(Account::getName,accountName);
        Account account = accountMapper.selectOne(qw);

        if(Objects.isNull(account)){
            return R.error("扣款账户不存在");
        }

        BigDecimal jinecount;
        BigDecimal zhuanghucount = account.getCount();
        if(StringUtils.isEmpty(count)){
            return R.error("扣款金额不可为空");
        }
        jinecount = new BigDecimal(count);
        BigDecimal zuizhong = zhuanghucount.subtract(jinecount);
        if(zuizhong.compareTo(BigDecimal.ZERO) <= 0){
            return R.error("扣款账户余额不足");
        }
        // 扣款业务 完成
        account.setCount(zuizhong);
        accountMapper.updateById(account);

        // 增加退款记录
        AccountFlow accountFlow = new AccountFlow();
        accountFlow.setAccountId(account.getId());
        accountFlow.setAccountName(accountName);
        accountFlow.setPayment(payment);
        accountFlow.setDesctext(desctext);
        accountFlow.setYve(account.getCount());
        accountFlow.setCount(jinecount.negate());// 变负数保存
        accountFlowMapper.insert(accountFlow);
        return R.success(account);
    }

    public Long getStuid() {
        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        String left = sdf.format(date);
        System.out.println(left);
        LambdaQueryWrapper<StuA> qw = new LambdaQueryWrapper<>();
        qw.like(StuA::getSid, left);
        Integer count = stuAMapper.selectCount(qw);
        long result;
        if (count > 0) {
            String sid = stuAMapper.getSid();
            long i = Long.parseLong(sid);
            i = i + 1;
            result = i;
        } else {
            String re = left + "001";
            result = Long.parseLong(re);
        }
        return result;
    }
}
